package service.impl;

import domain.VideoCategory;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import service.IVideoCategoryService;

/**
 *
 * @author yuyok
 */
@Stateless
@LocalBean
public class VideoCategoryServiceImpl extends GenericServiceImpl<VideoCategory> implements IVideoCategoryService {

    @PersistenceContext(unitName = "onlinetvPU")
    private EntityManager em;

    public VideoCategoryServiceImpl() {
        super(VideoCategory.class);
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    @Override
    public VideoCategory getVideoCategoryByName(String name) throws IllegalArgumentException {
        if (name == null) {
            throw new IllegalArgumentException("Category name cannot be null while getting by this field");
        }

        Query query = getEntityManager().createNamedQuery("getVideoCategoryByName");
        query.setParameter("name", name);

        try {
            return (VideoCategory) query.getSingleResult();
        } catch (NoResultException noRes) {
            return null;
        }
    }

//    @Override
//    protected void beforeEntityDelete(VideoCategory entity) {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
    @Override
    public List<VideoCategory> getAllVideoCategories() {
        Query query = getEntityManager().createNamedQuery("getAllVideoCategories");

        return query.getResultList();
    }
}
